/**
 * @param {number[]} nums
 * @return {boolean}
 */
export var isMonotonic = function (nums) {
  let [inc, dec] = [true, true]
  for (let i = 1; i < nums.length; i++) {
    if (nums[i] > nums[i - 1]) {
      dec = false
    }
    if (nums[i] < nums[i - 1]) {
      inc = false
    }
  }
  return inc || dec
}

/**
 * @param {number[]} nums
 * @return {boolean}
 */
export var isMonotonic2 = function (nums) {
  let [flag, ans] = [0, true]
  for (let i = 1; i < nums.length; i++) {
    if (flag === 0) {
      if (nums[i] > nums[i - 1]) {
        flag = 1
      } else if (nums[i] < nums[i - 1]) {
        flag = -1
      }
    } else {
      if (flag === 1 && nums[i] < nums[i - 1]) return false
      if (flag === -1 && nums[i] > nums[i - 1]) return false
    }
  }
  return ans
}

/**
 * 和方法二一样(简化代码)
 * @param {number[]} nums
 * @return {boolean}
 */
export var isMonotonic3 = function (nums) {
  let [flag, ans] = [0, true]
  for (let i = 1; i < nums.length; i++) {
    if (flag === 0) {
      flag = nums[i] > nums[i - 1] ? 1 : nums[i] < nums[i - 1] ? -1 : 0
      continue
    }
    if (flag === 1 && nums[i] < nums[i - 1]) return false
    if (flag === -1 && nums[i] > nums[i - 1]) return false
  }
  return ans
}
